class smx_os {
  $base_packages_list = [
    'rsync',
    'wget',
    'unzip',
    'bzip2',
#    'mod_ssl',
    'gcc-c++',
    'libstdc++-devel',
    'cmake',
    'gcc',
    'git',
    'pcre-devel',
    'openssl',
    'openssl-devel',
    'mlocate',
    'libxml2-devel',
    'nfs-utils',
    'ntp',
    'rpcbind',
    'initscripts',
    'rsyslog',
    'yum-plugin-priorities'
  ]

  $absent_packages_list = [
    'php-pecl-mongo',
    'php-mysql',
  ]

  # Since yumrepo resource doesn't support multi repos under one file, hard coded the unneeded extra repo files here

  #  $absent_repo_list = [
  #    '/etc/yum.repos.d/CentOS-Mongodb32.repo',
  #    '/etc/yum.repos.d/CentOS-Mongodb64.repo',
  #    '/etc/yum.repos.d/cloudera.repo',
  #    '/etc/yum.repos.d/epel-apache-maven.repo',
  #  ]

  #  file { $absent_repo_list:
  #    ensure => absent,
  #    before => Yumrepo[remi],
  #  }

  #if $operatingsystemrelease =~ /^6\./ {
  #  yumrepo { "remi":
  #    mirrorlist => "http://rpms.famillecollet.com/enterprise/6/remi/mirror",
  #    descr      => 'Les RPM de remi pour Enterprise Linux 6 - $basearch',
  #    enabled    => 1,
  #    gpgcheck   => 0
  #  }
  #}
  #elsif $operatingsystemrelease =~ /^7\./ {
    yumrepo { "remi":
      mirrorlist => "http://rpms.famillecollet.com/enterprise/7/remi/mirror",
      descr      => 'Les RPM de remi pour Enterprise Linux 7 - $basearch',
      enabled    => 1,
      priority => 2,
      gpgcheck   => 0
    }
  #}
  #else {
  #  notify { "Some other operating system detected": }
  #}

  if $operatingsystemrelease =~ /^6\./ {
    service { 'iptables':
      ensure => stopped,
      enable => false, }
  }
  elsif $operatingsystemrelease =~ /^7\./ {
    service { 'firewalld':
      ensure => stopped,
      enable => false, }
  }
  else {
    notify { "Unrecognized operating system": }
  }
  package { 'nscd' :
    ensure => latest,
  }

  service { 'nscd' :
    ensure  => running,
    before  => Package[$base_packages_list],
    require => Package['nscd'],
  }

  package { $absent_packages_list :
    ensure => absent,
    notify => Package[$base_packages_list],
  }

  # git and perl-Git have circular dependencies. Have to add --nodeps to remove package git
  define rmpack {
    exec { 'remove_${name}' :
      command => "rpm -e ${name} --nodeps",
      path    => "/bin",
      onlyif  => "rpm -q --quiet ${name}",
    }
  }

  # Some new Yii/Bower stuff needs to run git, for some reason
  #   rmpack { "git" : }

  package { $base_packages_list :
    ensure  => installed,
    before  => Notify['pkg-install'],
  #  require => Yumrepo['remi'],
  }

  notify { 'pkg-install':
    message => '----- Base pkgs installation completed -----',
  }

  #  file { '/etc/resolv.conf':
  #    ensure => file,
  #    group => root, owner => root, mode => 644,
  #    content => template('smx_os/resolv.conf.erb'),
  #  }

  file { '/etc/localtime':
    ensure => '/usr/share/zoneinfo/UTC',
    owner  => root, group => root,
  }

  file { '/etc/sysconfig/clock':
    ensure  => file,
    group   => root, owner => root,
    content => "ZONE=\"UTC\"\nUTC=True\n",
  }

  # user { "vagrant":
  #   ensure => present,
  #   gid  => 'vagrant',
  #   shell => '/bin/bash',
  #   home => '/home/vagrant',
  #   managehome => true,
  #   require => Group['vagrant'],
  # }

  # group { "vagrant":
  #   ensure => present,
  # }

  exec { "add vagrant user":
    command => 'useradd -d /home/vagrant -s /bin/bash -g vagrant -u 1002 vagrant',
    path    => ['/usr/bin','/usr/sbin','/bin','/sbin'],
    unless  => 'grep vagrant /etc/passwd',
    require => Exec['add vagrant group'],
  }

  exec { "add vagrant group":
    command => 'groupadd -g 1002 vagrant',
    path    => ['/usr/bin','/usr/sbin','/bin','/sbin'],
    unless  => 'grep vagrant /etc/group',
  }

  if $operatingsystemrelease =~ /^6\./ {
    service { 'nfs':
      enable  => true,
      ensure  => running,
      require => [ Package['nfs-utils'], Service['rpcbind'] ],
    }
  }
  elsif $operatingsystemrelease =~ /^7\./ {
    service { 'nfs-server':
      enable  => true,
      ensure  => running,
      require => [ Package['nfs-utils'], Service['rpcbind'] ],
    }
  }
  else {
    notify { "Unrecognized operating system": }
  }

  service { 'rpcbind':
    enable  => true,
    ensure  => running,
    require => Package['rpcbind'],
  }

  file { '/etc/yum.repos.d/smx-collection.repo':
    ensure => present,
    mode   => 755, owner => root, group => root,
    source =>  "puppet:///modules/smx_os/smx-collection.repo",
  }

  file { '/etc/foxitcloud':
    ensure => directory,
    before => File['/etc/foxitcloud/main.properties'],
  }

  if $fqdn =~ /^[a-z0-9]+\.[a-z0-9-]+\.foxitcloud\.com/ {
    file { '/etc/foxitcloud/main.properties' :
      ensure  => file,
      mode    => 644, group => root, owner => root,
      content => template('smx_os/main.properties.erb'), }
  }
  else {
    file { '/etc/foxitcloud/main.properties' :
      ensure => file,
      mode   => 644, group => root, owner => root,
      source => "puppet:///modules/smx_os/main.properties", }
  }

  file { '/vagrant/vagrant/functions.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/functions.sh",
    require => File['/vagrant/vagrant'],
  }
  file { '/root/hack_hostname.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/hack_hostname.sh",
    require => File['/vagrant/vagrant'],
  }

  file { '/vagrant/vagrant/_setup_nginx.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/_setup_nginx.sh",
    require => File['/vagrant/vagrant'],
  }


  file { '/vagrant/vagrant/createdb.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/createdb.sh",
    require => File['/vagrant/vagrant'],
  }

  file { '/vagrant/vagrant/prepare_composer.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/prepare_composer.sh",
    require => File['/vagrant/vagrant'],
  }

  file { '/vagrant/vagrant/non_windows_shell.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/non_windows_shell.sh",
    require => File['/vagrant/vagrant'],
  }

  file { '/vagrant/vagrant/windows_shell.sh':
    ensure  => present,
    mode    => 755,
    source  =>  "puppet:///modules/smx_os/windows_shell.sh",
    require => File['/vagrant/vagrant'],
  }

  file { '/vagrant/vagrant/servicelayer.sh':
    ensure  => present,
    mode    => 755,
    #    owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/servicelayer.sh",
    require => File['/vagrant/vagrant'],
  }

  file { '/vagrant':
    ensure => directory,
    mode   => 755,
    #    owner => root, group => root,
  }

  file { '/vagrant/vagrant':
    ensure => directory,
    mode   => 755,
    #    owner => root, group => root,
  }

  file { ["/root/svcconfig","/root/svcconfig/id_rsa"]:
    ensure => directory,
    mode   => 755,
    owner => root, group => root,
  }


  file { '/root/svcconfig/id_rsa/id_rsa':
    ensure => file,
    mode   => 600,
    owner => root, group => root,
    require => File["/root/svcconfig/id_rsa"],
    source =>  "puppet:///modules/smx_os/id_rsa",
  }

  file { '/etc/ssh/ssh_config':
    ensure  => file,
    mode    => 611, owner => root, group => root,
    source  =>  "puppet:///modules/smx_os/ssh_config",
    require => Package[$base_packages_list],
  }

  file { '/usr/share/ruby/vendor_ruby/facter/get_server.rb':
    ensure => file,
    mode   => 644, owner => root, group => root,
    source => "puppet:///modules/smx_os/get_server.rb",
  }

  exec { 'remove must have a tty to run sudo':
    command => "sed -i.bak '56s/^/#/' /etc/sudoers",
    path    => ["/usr/bin","/usr/sbin","/sbin","/bin"],
    unless  => ["test -f /etc/sudoers.bak"],
    require => Package[$base_packages_list],
  }
  exec { 'disabled selinux':
    command => "sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config",
    path    => ["/usr/bin","/usr/sbin","/sbin","/bin"],
    onlyif  => "test -f /etc/selinux/config",
  }
  service { 'rsyslog':
    ensure  => running,
    enable  => true,
  }
  ## Sending syslog from Linux systems into Graylog
  ## refer:https://marketplace.graylog.org/addons/a47beb3b-0bd9-4792-a56a-33b27b567856
  case $ipaddress {
    '10.1.103.110': { $string = "*.* @graylog.foxitcloud.com:1514;RSYSLOG_SyslogProtocol23Format" }
    '10.1.103.145': { $string = "*.* @graylog.foxitcloud.com:1514;RSYSLOG_SyslogProtocol23Format" }
    default: { $string = "#graylog" }
  }
  exec { "graylog" :
    command => "sed -i -e '\$a $string' /etc/rsyslog.conf",
    path    => [ "/bin","/sbin","/usr/bin","/usr/sbin" ],
    notify  => Service["rsyslog"],
    unless  => "grep graylog /etc/rsyslog.conf",
    require => Package[$base_packages_list],
  }
}
